package org.zoomdev.zoom;

import java.util.Random;

public class RandomEmailGenerator {
    private static final String[] COMMON_DOMAINS = {
            "gmail.com", "yahoo.com", "hotmail.com", "outlook.com",
            "icloud.com", "protonmail.com", "mail.com", "aol.com",
            "zoho.com", "yandex.com", "qq.com", "163.com"
    };

    private static final String[] COMMON_PREFIXES = {
            "john", "jane", "mike", "sarah", "david", "emma", "alex", "lisa",
            "robert", "jennifer", "william", "olivia", "michael", "sophia"
    };

    private static final String[] SEPARATORS = {"", ".", "_", "-"};
    private static final String[] DOMAIN_PREFIXES = {"", "mail.", "email.", "my.", "web."};
    private static final String[] COUNTRY_TLDS = {"", ".us", ".uk", ".ca", ".au", ".de", ".jp", ".cn"};

    private static final Random random = new Random();

    public static String generateRandomEmail() {
        // 随机选择生成方式：简单邮箱或复杂邮箱
        if (random.nextBoolean()) {
            return generateSimpleEmail();
        } else {
            return generateComplexEmail();
        }
    }

    public static void main(String[] args) {
        // 生成并打印10个随机邮箱
        System.out.println("随机生成的邮箱地址示例:");
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomEmail());
        }
    }

    private static String generateSimpleEmail() {
        // 简单邮箱格式：前缀@域名
        String prefix = COMMON_PREFIXES[random.nextInt(COMMON_PREFIXES.length)];
        String domain = COMMON_DOMAINS[random.nextInt(COMMON_DOMAINS.length)];

        // 50%概率在前缀后添加数字
        if (random.nextBoolean()) {
            prefix += random.nextInt(100);
        }

        return prefix + "@" + domain;
    }

    private static String generateComplexEmail() {
        // 复杂邮箱格式：前缀+分隔符+数字/字母+@+子域名+域名+国家代码
        StringBuilder email = new StringBuilder();

        // 前缀部分
        if (random.nextBoolean()) {
            // 使用常见前缀
            email.append(COMMON_PREFIXES[random.nextInt(COMMON_PREFIXES.length)]);
        } else {
            // 生成随机前缀 (3-8个字符)
            int prefixLength = random.nextInt(6) + 3;
            for (int i = 0; i < prefixLength; i++) {
                email.append((char) ('a' + random.nextInt(26)));
            }
        }

        // 50%概率添加分隔符和额外部分
        if (random.nextBoolean()) {
            String separator = SEPARATORS[random.nextInt(SEPARATORS.length)];
            email.append(separator);

            // 添加数字或额外字母
            if (random.nextBoolean()) {
                // 数字 (1-4位)
                email.append(random.nextInt(10000));
            } else {
                // 额外字母 (1-3个字符)
                int extraLength = random.nextInt(3) + 1;
                for (int i = 0; i < extraLength; i++) {
                    email.append((char) ('a' + random.nextInt(26)));
                }
            }
        }

        // @符号
        email.append("@");

        // 域名部分
        // 30%概率添加域名前缀
        if (random.nextFloat() < 0.3) {
            email.append(DOMAIN_PREFIXES[random.nextInt(DOMAIN_PREFIXES.length)]);
        }

        // 主域名
        email.append(COMMON_DOMAINS[random.nextInt(COMMON_DOMAINS.length)]);

        // 20%概率添加国家代码
        if (random.nextFloat() < 0.2) {
            email.append(COUNTRY_TLDS[random.nextInt(COUNTRY_TLDS.length)]);
        }

        return email.toString();
    }
}